package com.fbt.energy.util;

import com.fbt.energy.entity.AO.AmmeterDataAO;
import com.fbt.energy.entity.Ammeter;
import com.fbt.energy.service.AmmeterService;
import com.fbt.energy.service.impl.AmmeterData;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class Schedule {

    @Resource
    AmmeterService ammeterService;
    @Resource
    AmmeterData ammeterData;

    @Scheduled(cron = "0 1 * * * ? ")
    public void work() {
        System.out.println("启动就执行的定时任务！--------每一小时一分钟执行一次" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
     //   insertAmmeter();
        System.out.println("插入数据结束");

    }

    //插入电表数据
    public void insertAmmeter() {
        List<AmmeterDataAO> ammeterDataAOS = AmmeterData.HistoricalData();
        // double megawattHour = getMegawattHour();
        System.out.println("查询出来的原始数据     " + ammeterDataAOS);
        List<List<Double>> lists = peakPinggu();
        for (int i = 0; i < ammeterDataAOS.size(); i++) {
            AmmeterDataAO AO = ammeterDataAOS.get(i);
            List<Double> doubleList = lists.get(i);

            Double electric=0.0;
            Double peakHour = 0.0;
            Double peakPeriod = 0.0;
            Double peacetimePeriod = 0.0;
            Double valleyPeriod = 0.0;
            String mid = AO.getMid();
            if (mid.equals("1854416")) {
                 electric = doubleList.get(0);
            }else {

                electric = doubleList.get(0);
                peakHour = doubleList.get(1);
                peakPeriod = doubleList.get(2);
                peacetimePeriod = doubleList.get(3);
                valleyPeriod = doubleList.get(4);
            }

            String addtime = AO.getAddtime();
            String dsp = AO.getDsp();
            String address = AO.getAddress();

            String consume = AO.getConsume();
            String id = AO.getId();
            String uid = AO.getUid();


            Ammeter ammeter = new Ammeter();

            if (mid.equals("1854416")) {
               ammeter.setElectricQuantity(electric);
                ammeter.setPeakHour(peakHour);
                ammeter.setPeakPeriod(peakPeriod);
                ammeter.setPeacetimePeriod(peacetimePeriod);
                ammeter.setValleyPeriod(valleyPeriod);
            }else {
                ammeter.setElectricQuantity(electric);
                ammeter.setPeakHour(peakHour);
                ammeter.setPeakPeriod(peakPeriod);
                ammeter.setPeacetimePeriod(peacetimePeriod);
                ammeter.setValleyPeriod(valleyPeriod);

            }

            ammeter.setAmmeterNo(mid);
            ammeter.setAmmeterAddress(address);
            ammeter.setQuervTime(addtime);
        //    ammeter.setRemark(dsp);
             ammeter.setIndication(dsp);
            switch (mid) {
                case "1893952":
                    ammeter.setEquipmentNo("01");
                    ammeter.setAmmeterName("52号总表");
                    break;
                case "1893961":
                    ammeter.setEquipmentNo("02");
                    ammeter.setAmmeterName("61号分表");
                    break;
                case "1854416":
                    ammeter.setEquipmentNo("03");
                    ammeter.setAmmeterName("16号吾悦广场表");
                    break;
                default:
            }

            ammeter.setProductionNo("1");
            ammeter.setExt3("1号产线");


            ammeter.setElectricQuantity(electric);


            System.out.println("插入电表的数据" + ammeter.toString());
            ammeterService.insert(ammeter);
            System.out.println("插入数据成功");
        }


    }


    public static List<List<Double>> peakPinggu() {
        List<AmmeterDataAO> HistoricalData = AmmeterData.HistoricalData();
        List<AmmeterDataAO> lastHourDate = AmmeterData.LastHourDate();
        List<List<Double>> doubleListS = new ArrayList<>();
        for (int i = 0; i < HistoricalData.size(); i++) {
            AmmeterDataAO histS = HistoricalData.get(i);
            AmmeterDataAO lestS = lastHourDate.get(i);


            List<String> histSData = histS.getData();
            String addtime = histS.getAddtime();
            System.out.println("histSData   没有经过处理的  当前  小时   "+addtime+"   数据    "+histSData);
            List<String> lestSData = lestS.getData();

            String addtime1 = lestS.getAddtime();
            System.out.println("lestSData  没有经过处理的前 一个小时"+addtime1+"数据    "+lestSData);
            List<Double> doubleList = new ArrayList<>();
            for (int j = 0; j < histSData.size(); j++) {
                String hist = histSData.get(j);
                String list = lestSData.get(j);

                Double histDouble = Double.parseDouble(hist);
                Double listDouble = Double.parseDouble(list);

                double sub = Arith.sub(histDouble, listDouble);

                doubleList.add(sub);
            }
            doubleListS.add(doubleList);
            System.out.println("转换过后的尖 峰 平 谷" + doubleList);
        }

        return doubleListS;

    }

    //每小时用电量
    public static double getMegawattHour() {
        List<AmmeterDataAO> HistoricalData = AmmeterData.HistoricalData();
        List<String> HistDsp = HistoricalData.stream().map(AmmeterDataAO::getDsp).collect(Collectors.toList());
        String histDataS = HistDsp.get(0).replace(" kWh", "").trim();
        Double histData = Double.parseDouble(histDataS);
        System.out.println("截至当前一小时历史用电量" + histData);


        List<AmmeterDataAO> lastHourDate = AmmeterData.LastHourDate();
        List<String> lestDsp = lastHourDate.stream().map(AmmeterDataAO::getDsp).collect(Collectors.toList());

        String LastDataS = lestDsp.get(0).replace(" kWh", "").trim();
        Double LastData = null;
        LastData = Double.parseDouble(LastDataS);
        System.out.println("截至上一个小时历史用电量----" + LastData);

        System.out.println("_________________________________________-");

        double megawattHour = Arith.sub(histData, LastData);
        System.out.println("上一个小时真实用电量-----》" + megawattHour);

        return megawattHour;
    }


    //查询用电历史数据
    public static void queryElectricity() {
        List<AmmeterDataAO> ammeterDataAOS = AmmeterData.HistoricalData();
        for (AmmeterDataAO AO : ammeterDataAOS) {
            String addtime = AO.getAddtime();
            String dsp = AO.getDsp();
            String address = AO.getAddress();
            String mid = AO.getMid();
            String consume = AO.getConsume();
            String id = AO.getId();
            System.out.println("电表地址:	" + address + " 总用电量：	" + dsp + " 查询时间：	" + addtime + "  电表标识：  " + mid + "  采集用时:	" + consume + "  数据唯一id:	 " + id);
        }


    }

    //查询前一个小时的用电量
    public static void lastHourDate() {
        List<AmmeterDataAO> lastHourDate = AmmeterData.LastHourDate();
        for (AmmeterDataAO lstAO : lastHourDate) {
            String addtime = lstAO.getAddtime();
            String dsp = lstAO.getDsp();
            String address = lstAO.getAddress();
            String mid = lstAO.getMid();
            String consume = lstAO.getConsume();
            String id = lstAO.getId();
            System.out.println("电表地址:	" + address + " 一个小时前总用电量：	" + dsp + " 查询时间：	" + addtime + "电表标识：  " + mid + "采集用时:	" + consume + "数据唯一id:	" + id);

        }
    }
}